﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Contrib.Taxonomies.Models;
using Contrib.Taxonomies.Services;
using Orchard.ContentManagement;
using Orchard.Environment;
using Orchard.Environment.Extensions.Models;

namespace Barry.Test.Handler
{
    public class ProductFeatureEventHandler : IFeatureEventHandler
    {
        private readonly ITaxonomyService _taxonomyService;
        private readonly IContentManager _contentManager;

        public ProductFeatureEventHandler(ITaxonomyService taxonomyService, IContentManager contentManager) {
            _taxonomyService = taxonomyService;
            _contentManager = contentManager;
        }

        public void Installing(Feature feature) {
          //  throw new NotImplementedException();
        }

        public void Installed(Feature feature) {
           
        }

        private void CreateTerm(TaxonomyPart catog, string catogName) {
            var term = _taxonomyService.NewTerm(catog);
            term.Name = catogName;
            _contentManager.Create(term, VersionOptions.Published);
        }

        public void Enabling(Feature feature) {
            //  throw new NotImplementedException();
        }

        public void Enabled(Feature feature) {
            //  throw new NotImplementedException();
            //  throw new NotImplementedException();
            if (_taxonomyService.GetTaxonomyByName("Catogory") == null)
            {
                var catog = _contentManager.New<TaxonomyPart>("Taxonomy");
                catog.Name = "Catogory";
                _contentManager.Create(catog, VersionOptions.Published);
                CreateTerm(catog, "Food");
                CreateTerm(catog, "Water");
                CreateTerm(catog, "Fruit");
                CreateTerm(catog, "Electronic");


            }
        }

        public void Disabling(Feature feature) {
            //  throw new NotImplementedException();
        }

        public void Disabled(Feature feature) {
            //throw new NotImplementedException();
        }

        public void Uninstalling(Feature feature) {
            // throw new NotImplementedException();
        }

        public void Uninstalled(Feature feature) {
            //  throw new NotImplementedException();
        }
    }
}